/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.table.cellRenderer;

import java.awt.Color;
import java.awt.Component;
import java.util.Map;
import java.util.TreeMap;

import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import org.jxUtils.collection.CollectionUtil;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.table.cellRenderer
 * @date 04/08/2014 - 19:43:34
 */
public class PercentageRenderer extends JProgressBar implements TableCellRenderer {
	
	private static final long serialVersionUID = 8449276836513739962L;
	
	private Map<Integer, Color> limitColors = new TreeMap<>();
	
	private int[] limitValues;
	
	/**
	 * Creates a progress bar with the specified orientation,
	 * which can be
	 * either {@code SwingConstants.VERTICAL} or {@code SwingConstants.HORIZONTAL}.
	 * By default, a border is painted but a progress string is not.
	 * The initial and minimum values are 0,
	 * and the maximum is 100.
	 * 
	 * @param orient
	 *        the desired orientation of the progress bar
	 * @throws IllegalArgumentException
	 *         if {@code orient} is an illegal value
	 * @see #setOrientation
	 * @see #setBorderPainted
	 * @see #setStringPainted
	 * @see #setString
	 * @see #setIndeterminate
	 */
	public PercentageRenderer() {
	
		super();
		
		initGUI();
	}
	
	/**
	 * 
	 */
	private void changeLimitColors() {
	
		limitValues = new int[limitColors.size()];
		
		int index = 0;
		
		//
		for(final int limit : limitColors.keySet()) {
			
			limitValues[index++] = limit;
		}
		
		CollectionUtil.sort(limitValues);
	}
	
	/**
	 * @param value
	 * @return Color or null
	 */
	private Color getColor(final int value) {
	
		Color color = null;
		
		// 
		if(limitValues != null) {
			
			//
			for(final int limit : limitValues) {
				
				//
				if(limit < value) {
					
					color = limitColors.get(limit);
				}
			}
		}
		
		return color;
	}
	
	/**
	 * 
	 */
	private void initGUI() {
	
		showStringValue(true);
		setBorderPainted(false);
		
		final int limit = new Double(getMaximum() / 3).intValue();
		
		limitColors.put(new Integer(getMinimum()), Color.GREEN);
		limitColors.put(new Integer(limit), Color.ORANGE);
		limitColors.put(new Integer(limit * 2), Color.RED);
		
		changeLimitColors();
	}
	
	/**
	 * @param limit
	 * @param color
	 */
	public void addLimitColor(final int limitValue, final Color color) {
	
		limitColors.put(limitValue, color);
		
		changeLimitColors();
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
	 */
	@Override
	public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column) {
	
		int number = 0;
		
		//
		if(value != null) {
			
			//
			if( !(value instanceof Number)) {
				
				try {
					//
					number = Integer.valueOf(String.valueOf(value).trim());
					
				}catch(final NumberFormatException numberFormatException) {
					// ignore
				}
				
			}else {
				
				number = ((Number)value).intValue();
			}
		}
		
		final Color color = getColor(number);
		
		//
		if(color != null) {
			
			setForeground(color);
		}
		
		//
		setBackground(table.getBackground());
		setValue(number);
		
		return this;
	}
	
	/**
	 * @param limitColors
	 */
	public void setLimitColor(final Map<Integer, Color> limitColors) {
	
		this.limitColors = limitColors;
		
		changeLimitColors();
	}
	
	/**
	 * @param show
	 */
	public void showStringValue(final boolean show) {
	
		setStringPainted(show);
	}
}
